package com.example.administrator.yunniuusedcar.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.administrator.yunniuusedcar.adapter.ContactAdapter;
import com.example.administrator.yunniuusedcar.bean.Brand;
import com.example.administrator.yunniuusedcar.bean.MoreBrand;
import com.example.administrator.yunniuusedcar.model.Contact;
import com.example.administrator.common.utils.Constant;
import com.example.administrator.yunniuusedcar.model.mode_benn.IHttpService;
import com.example.administrator.yunniuusedcar.view.LetterBar;
import com.example.administrator.yunniuusedcar.R;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import okhttp3.Call;

/**
 *
 * 选车   --》更多功能
 */
    // 选择 车型  更多

public class ChooseCarModelsActivity extends Activity {

    private TextView tvLetter;
    private ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_choose_car_models);
        tvLetter = (TextView) findViewById(R.id.tv_letter);

        initLetterBar();
        initListView();
        initData();
    }




    ArrayList<Contact> listDatas;

    private void initListView() {
        listView = (ListView) findViewById(R.id.list_view);

    }

    private void initLetterBar() {
        LetterBar letterBar = (LetterBar) findViewById(R.id.letter_bar);

        letterBar.setTextView(tvLetter);

        letterBar.setOnLetterSelectedListener(new LetterBar.OnLetterSelectedListener() {
            @Override // 用户选中某个字母
            public void onLetterSelected(String letter, int index) {
                // System.out.println("-----------选中的字母：" + letter);
                for (int i = 0; i < listDatas.size(); i++) {
                    Contact context = listDatas.get(i);
                    if (letter.equals(context.firstLetter)) {
                        // 让列表滚动到指定的位置
                        listView.setSelection(i);
                        return;
                    }
                }
            }
        });
    }

    private void initData() {
        OkHttpUtils.post().url( IHttpService.URL2 + IHttpService.GETLETTERBRAND)
                .addParams("sign", IHttpService.SignVal)
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {

            }

            @Override
            public void onResponse(String response, int id) {
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    String data = jsonObject.getString("data");


                    Gson gson = new Gson();
                    Type type = new TypeToken<List<MoreBrand>>() {
                    }.getType();
                    List<MoreBrand> beanOnes = gson.fromJson(data, type);


                            // 准备列表数据
                    listDatas = new ArrayList<>();
                    for(int i=0;i<beanOnes.size();i++){
                        for(int j=0;j<beanOnes.get(i).getBrand().size();j++){
                            Brand brand = beanOnes.get(i).getBrand().get(j);
                            Contact contact = new Contact(brand.getName(),brand.getBrand_id(),brand.getLogo());
                            listDatas.add(contact);
                        }
                    }
                        // 排序操作
                     Collections.sort(listDatas, new Comparator<Contact>() {
                     @Override
                    public int compare(Contact o1, Contact o2) {
                    // 按拼音升序, 取负号则为降序
                    return o1.pinyin.compareTo(o2.pinyin);
                    }
                });

                    // 显示列表
                ContactAdapter contactAdapter = new ContactAdapter(getApplicationContext(), listDatas);
                listView.setAdapter(contactAdapter);
                listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                        int brand_id = listDatas.get(i).brand_id;
                        Intent intent =getIntent();
                        //这里使用bundle绷带来传输数据
                        Bundle bundle =new Bundle();
                        //传输的内容仍然是键值对的形式
                        bundle.putInt("brand_id",brand_id);//回发的消息,hello world from secondActivity!
                        bundle.putString("brand_name",listDatas.get(i).name);
                        intent.putExtras(bundle);
                        setResult(RESULT_OK,intent);
                        finish();
                    }
                });

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

}

